package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.api.stock.CancelOrderServiceAPI;
import com.steel.yfmall.cloud.assistant.bean.param.APIParamCancelOrderBean;
import com.steel.yfmall.cloud.assistant.po.view.OutCallApiLogBean;
import com.steel.yfmall.cloud.components.mq.bean.RequestSendMQBean;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.oms.manager.api.ManageOrderQueryService;
import com.steel.yfmall.cloud.oms.manager.api.bean.para.RequestOrderQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: CancelOrderServiceAPIImpl
 * @author: liuzhijun
 * @date: 2021-04-05 15:33
 **/
@Service
@Slf4j
public class CancelOrderServiceAPIImpl implements CancelOrderServiceAPI {


    @Override
    public APIMsgBean<Boolean> cancelOrder(APIParamCancelOrderBean apiParamCancelOrderBean) {
        APIMsgBean<Boolean> booleanAPIMsgBean = new APIMsgBean<>();
        booleanAPIMsgBean.setData(false);
        booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean());
        if (apiParamCancelOrderBean == null) {
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return booleanAPIMsgBean;
        }
        if (StringUtils.isBlank(apiParamCancelOrderBean.getMasterOrderSn())) {
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("销售订单号不能为空"));
            return booleanAPIMsgBean;
        }
        try {
            RequestOrderQuery requestOrderQuery = new RequestOrderQuery();
            requestOrderQuery.setMasterOrderSn(apiParamCancelOrderBean.getMasterOrderSn());
            requestOrderQuery.setCode("11");
            requestOrderQuery.setSiteCode("SDYFGT");
            requestOrderQuery.setSource("POS");
            requestOrderQuery.setCancelReason(apiParamCancelOrderBean.getCancelReason());
            log.info("调用manageOrderQueryService.cancelOrderByMasterSn接口参数："+ JSON.toJSONString(requestOrderQuery));
            booleanAPIMsgBean = manageOrderQueryService.cancelOrderByMasterSn(requestOrderQuery);

            //记录日志
            OutCallApiLogBean outCallApiLogBean = new OutCallApiLogBean();
            outCallApiLogBean.setApiUrl("cancelOrder");
            outCallApiLogBean.setParaText(JSON.toJSONString(apiParamCancelOrderBean));
            outCallApiLogBean.setInterfaceMethod("cancelOrder");
            outCallApiLogBean.setInterfaceMethodName("苍穹调用电商订单取消");
            outCallApiLogBean.setResultText(JSON.toJSONString(booleanAPIMsgBean));
            RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
            requestSendMQBean.setSendMqTitle("insert_out_call_api_log");
            requestSendMQBean.setSendMqMessage(JSON.toJSONString(outCallApiLogBean));
            mqSendService.sendCommonMQMessage(requestSendMQBean);

        } catch (Exception e) {
            log.info("订单取消异常:" + e);

        }
        return booleanAPIMsgBean;
    }


    @Resource
    private ManageOrderQueryService manageOrderQueryService;

    @Resource
    private MqSendService mqSendService;
}
